#include "LinearRegression.h"
#include <vector>

using namespace std;
using std::vector;

LinearRegression::LinearRegression(vector<double> x, vector<double> y)
{
	this->x = x;
	this->y = y;
	this->h = * (new vector<double>(M()));
	this->errors = * (new vector<double>(M()));
}

int LinearRegression::M()
{
	return this->x.size();
}

double LinearRegression::J()
{
	double errorSum = 0;
	for(int i = 0; i < M(); i++)
	{
		//populate h vector
		h[i] = thetaZero + thetaOne * x[i];
		
		//populate error vector
		double error = y[i] - h[i];
		errors[i] = error*error;

		errorSum += errors[i];
	}

	return errorSum/(2*M());
}

void LinearRegression::SetThetas(double thetaZero, double thetaOne)
{
	this->thetaZero = thetaZero;
	this->thetaOne = thetaOne;
}
